package cn.itcast.work.springsecurity;

import cn.itcast.work.pojo.User;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * * @author: Mao Wanting 2022051602008
 * * @date: 2024/12/23
 * *description:自定义的认证用户细节
 **/
@EqualsAndHashCode(callSuper = true)
@Data
public class CustomUserDetails extends User implements UserDetails {

    private String username;
    private String userpassword;
    private String phone;
    private String email;
    private int id;
    private String role;

    private User user;
    private List<GrantedAuthority> authorities;


    public CustomUserDetails(User user) {
        this.username = user.getUsername();
        this.userpassword = user.getUserpassword();
        this.phone = user.getPhone();
        this.email = user.getEmail();
        this.id = user.getId();
        this.role = user.getRole();
        this.authorities= Collections.singletonList(new SimpleGrantedAuthority(user.getRole()));
    }

    @Override
    public String getUsername() {
        return username;
    }


    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return authorities;
    }

    @Override
    public String getPassword() {
        return userpassword;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
